﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FSS.Core.Controls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.Core.Util;
using log4net;

namespace SalesManagement.Modules.List
{
    public partial class ProductList : BasePage
    {
        public ProductList()
            : base(SCREENS.PRODUCTSLIST)
        {

        }        
        protected void Page_Load(object sender, EventArgs e)
        {
            base.CheckPermissionControl();
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
        }
        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            BindingProduct();
        }
        [DirectMethod]
        public void DocumentReady()
        {
            BindingProduct();
        }
        protected void BindingProduct()
        {
            ProductEntity productE = new ProductEntity();
            ProductService productS = ProductService.GetInstance();
            List<ProductEntity> listProduct=(List<ProductEntity>)productS.LoadAll();
            if(!listProduct.Equals(null))
            {
                storeMain.DataSource = listProduct;
                storeMain.DataBind();
            }
        }
        protected void btnAdd_Click(object sender, DirectEventArgs e) 
        {
            this.WindowEditor.Show();
        }
        protected void btnFind_Click(object sender, DirectEventArgs e) { }
        protected void grdMain_OnCommand(object sender, DirectEventArgs e)
        {
            int id = Int32.Parse(e.ExtraParams["id"]);
            string command = e.ExtraParams["command"];
            if (IsEdit && Constants.Edit.Equals(command))
            {
                this.WindowEditor.Show(id);
            }
            else if (IsDelete && Constants.Delete.Equals(command))
            {
                ProductEntity productE = new ProductEntity();
                ProductService productS = ProductService.GetInstance();
                productE.ProductId = id;
                if (productS.Delete(productE).Equals(0))
                    X.Msg.Notify("Lỗi", "Không xóa được").Show();
                BindingProduct();
            }
        }
        protected void grdMain_OnAfterEdit(object sender, DirectEventArgs e)
        {
            try
            {
                bool IsActive = bool.Parse(e.ExtraParams["IsActive"]);
                int id = int.Parse(e.ExtraParams["ProductId"]);
                ProductEntity productE = new ProductEntity();
                ProductService productS = ProductService.GetInstance();
                productE.IsActive = IsActive;
                productE.ProductId = id;
                productS.ProductUpdateCheck(productE);
            }
            catch (Exception ex)
            {
                X.Msg.Alert("Error", ex.Message).Show();
            }
        }
    }
}
